package com.tidb.example.demo;

import com.alibaba.druid.pool.DruidConnectionHolder;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.tidb.example.demo.entity.User;
import com.tidb.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.*;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author : ${author}
 * @version V1.0
 * @Package ${packageName}
 * @Description: todo
 * @date ${date} ${time}
 */
public class MyThread extends Thread {

//      private List<User> list;
      private CountDownLatch begin;
//      private CountDownLatch end;
//
//      private java.sql.Connection conn ;
//      private String sql = "insert into user_t(uname, gender, birthday) values(?,?,?)" ;


      //创建个构造函数初始化 list,和其他用到的参数
      public MyThread(CountDownLatch count) {
            this.begin = count ;
      }

      @Override
      public void run() {
            String url = "jdbc:mysql://168.60.12.52:4000/test?useUnicode=true&characterEncoding=utf-8";
            String name = "com.mysql.jdbc.Driver";
            String user = "root";
            String password = "";
            Connection conn = null;
            try {
                  Class.forName(name);
                  conn = DriverManager.getConnection(url, user, password);//获取连接
                  conn.setAutoCommit(false);//关闭自动提交，不然conn.commit()运行到这句会报错
            } catch (ClassNotFoundException e1) {
                  e1.printStackTrace();
            } catch (SQLException e) {
                  e.printStackTrace();
            }
            // 开始时间
            Long begin = System.currentTimeMillis();
            // sql前缀
            String prefix = "INSERT INTO user_t (uname,gender,birthday) VALUES ";
            try {
                  // 保存sql后缀
                  StringBuffer suffix = new StringBuffer();
                  // 设置事务为非自动提交
                  conn.setAutoCommit(false);
                  // 比起st，pst会更好些
                  PreparedStatement  pst = (PreparedStatement) conn.prepareStatement("");//准备执行语句
                  // 外层循环，总提交事务次数
                  for (int i = 1; i <= 20; i++) {
                        suffix = new StringBuffer();
                        // 第j次提交步长
                        for (int j = 1; j <= 10000; j++) {
                              // 构建SQL后缀
                              suffix.append("('小敏" +"','男',"+"'20180807'),");
                        }
                        // 构建完整SQL
                        String sql = prefix + suffix.substring(0, suffix.length() - 1);
                        // 添加执行SQL
                        pst.addBatch(sql);
                        // 执行操作
                        pst.executeBatch();
                        // 提交事务
                        conn.commit();
                        // 清空上一次添加的数据
                        suffix = new StringBuffer();
                  }
                  // 头等连接
                  pst.close();
                  conn.close();
            } catch (SQLException e) {
                  e.printStackTrace();
            }finally {
                  this.begin.countDown();
            }
            // 结束时间
            Long end = System.currentTimeMillis();
            // 耗时
            System.out.println("20万条数据插入花费时间 : " + (end - begin) / 1000 + " s"+"  插入完成");
      }
}
